import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/customwidget/container_widget/page/ItemPage.dart';
import 'package:flutter_app/util/ToastUtil.dart';

/// AppBar({
/// Key key,
/// this.leading, //导航栏最左侧Widget，常见为抽屉菜单按钮或返回按钮。
///  this.automaticallyImplyLeading = true, //如果leading为null，是否自动实现默认的leading按钮
///  this.title,// 页面标题
///  this.actions, // 导航栏右侧菜单
///  this.bottom, // 导航栏底部菜单，通常为Tab按钮组
/// this.elevation = 4.0, // 导航栏阴影
/// this.centerTitle, //标题是否居中
/// this.backgroundColor,
/// ...   //其它属性见源码注释
// })
class ScaffoldTabBarWidget2 extends StatefulWidget {
  ScaffoldTabBarWidget2({
    Key? key,
    @required this.text,
  }) : super(key: key);

  final String? text;

  @override
  State<StatefulWidget> createState() {
    return new ScaffoldState(text ?? "");
  }
}

class ScaffoldState extends State<ScaffoldTabBarWidget2>
    with SingleTickerProviderStateMixin {
  TabController? _tabController; //需要定义一个Controller
  List tabs = ["新闻", "历史", "图片"];

  String? title; //标题
  ScaffoldState(String? text) {
    title = text;
  }

  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //导航栏
        title: Text(title??""),
        bottom: TabBar(
          //生成Tab菜单
          controller: _tabController,
          tabs: tabs.map((e) => Tab(text: e)).toList(),
          onTap: _onItemTapped,
        ),
      ),
      body: TabBarView(
        //联动
        controller: _tabController,
        children: tabs.map((e) {
          //创建3个Tab页
          return Container(
            alignment: Alignment.center,
            child: Text(e, textScaleFactor: 5),
          );
        }).toList(),
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }
}
